<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="版本名称" prop="versionName">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入版本名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增版本</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column prop="id" align="center" v-if="false" />
      <el-table-column label="app类型" prop="appType" align="center" />
      <el-table-column label="版本名称" prop="versionName" align="center" />
      <el-table-column label="是否强制更新" prop="isForceUpdate" align="center" />
      <el-table-column label="版本号" prop="versionNo" align="center" />
      <el-table-column label="下载地址" prop="downloadAddr" align="center" />
      <el-table-column label="描述" prop="description" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            circle
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="500px"
      :before-close="handleClose"
    >
      <el-form
        :model="appVersionForm"
        ref="appVersionForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="app类型" prop="appType" >
          <el-select
            v-model="appVersionForm.appType"
            clearable
            filterable
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in appTypeOption"
              :key="item.value"
              :label="item.value"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否强制更新" prop="isForceUpdate" >
          <el-select
            v-model="appVersionForm.isForceUpdate"
            clearable
            filterable
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option value="1" label="是"> </el-option>
            <el-option value="0" label="否"> </el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item label="版本名称" prop="versionName" >
          <el-input
            style="width: 100%"
            v-model="appVersionForm.versionName"
            placeholder="请输入"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="版本号" prop="versionNo" >
          <el-input
            style="width: 100%"
            v-model="appVersionForm.versionNo"
            placeholder="请输入"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="下载地址" prop="downloadAddr" >
          <el-input
            style="width: 100%"
            v-model="appVersionForm.downloadAddr"
            placeholder="请输入"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description" >
          <el-input
            type="textarea"
            style="width: 100%"
            v-model="appVersionForm.description"
            placeholder="请输入"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSumit">提 交</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  createAppVersion,
  findAppVersionByPageList,
  deleteAppVersion,
} from "@/api/system/app";
import { number } from "echarts";

export default {
  name: "app",

  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //打开对话框
      dialogVisible: false,
      //表格数据
      dataList: {},
      //app类型
      appTypeOption: [
        {
          lable: "",
          value: "android",
        },
        {
          lable: "",
          value: "ios",
        },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        versionName: undefined,
      },
      appVersionForm: {
        appType: undefined,
        versionName: undefined,
        versionNo: undefined,
        downloadAddr: undefined,
        description: undefined,
      },
      rules: {
        appType: [{ required: true, message: "必填", trigger: "change" }],
        isForceUpdate: [{ required: true, message: "必填", trigger: "change" }],
        versionName: [{ required: true, message: "必填", trigger: "blur" }],
        versionNo: [
          { required: true, message: "必填", trigger: "blur" },
          { pattern: '^[0-9]*$', message: "必须为纯数字" ,trigger: "blur"},
        ],
        downloadAddr: [{ required: true, message: "必填", trigger: "blur" }],
      },
    };
  },

  created() {
    this.getList();
  },

  methods: {
    getList() {
      this.loading = true;
      findAppVersionByPageList(this.queryParams)
        .then((response) => {
          this.dataList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        })
        .catch((res) => {
          this.loading = false;
        });
    },

    /** 重置表单 */
    reset() {
      this.appVersionForm = {
        appType: undefined,
        versionName: undefined,
        versionNo: undefined,
        downloadAddr: undefined,
        description: undefined,
      };
      this.resetForm("appVersionForm");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置查询 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleAdd() {
      this.dialogVisible = true;
      this.reset(this.appVersionForm);
      this.title = "新增app版本";
    },

    /** 关闭弹窗 */
    handleClose(done) {
      this.dialogVisible = false;
    },

    handleSumit() {
      this.$refs["appVersionForm"].validate((valid) => {
        if (valid) {
          createAppVersion(this.appVersionForm).then((result) => {
            this.$message({ message: "提交成功", type: "success" });
            this.dialogVisible = false;
            this.getList();
          });
        }
      });
    },

    /** 删除配置 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除"' + row.appType + "--" + row.versionName + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return deleteAppVersion(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
  },
};
</script>

<style>
</style>